﻿Imports System.Web

Namespace HttpModule
    Public Class BlockIPHttpModule
        Implements IHttpModule

        Public Sub Dispose() Implements System.Web.IHttpModule.Dispose

        End Sub

        Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
            AddHandler context.BeginRequest, AddressOf app_BeginRequest
        End Sub

        Sub app_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
            Dim hash = DirectCast(HttpContext.Current.Application("blockip"), Hashtable)
            If hash IsNot Nothing AndAlso hash.Contains(HttpContext.Current.Request.UserHostAddress) Then
                HttpContext.Current.Response.Write("来源IP[" + HttpContext.Current.Request.UserHostAddress.ToString() + "]不在允许访问之内<br><br>请与管理员联系解决！")
                HttpContext.Current.Response.End()
            Else
                HttpContext.Current.Response.Write("恭喜，你的IP不在受限访问本网站之列！")
            End If
        End Sub
    End Class
End Namespace